package com.eleadmin.order.controller;

import com.eleadmin.common.core.web.*;
import com.eleadmin.common.core.annotation.OperationLog;
import com.eleadmin.damon.utils.results.PageResult2;
import com.eleadmin.order.entity.ContainOceanApply;
import com.eleadmin.order.entity.ContainerBill;
import com.eleadmin.order.param.ContainerBillParam;
import com.eleadmin.order.service.ContainerBillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 *
 * 拖车运单
 * @author : Doman
 * @date : 2023-01-13 11:14:35
 * @version ：1.0
 */

@Api(tags = "拖车运单")
@RestController
@RequestMapping("/api/order/containerBill")
public class ContainerBillController extends BaseController {

    @Autowired
    private ContainerBillService containerBillService;

    @PreAuthorize("hasAuthority('order:containerBill:list')")
    @ApiOperation("分页查询拖车运单")
    @GetMapping("/page")
    public JsonResult page(PageResult2<T> page, ContainerBillParam containerBill) throws Exception{
        return containerBillService.list(page, containerBill, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:containerBill:list')")
    @ApiOperation("查询全部拖车运单")
    @GetMapping()
    public JsonResult list(ContainerBillParam containerBill){
        return containerBillService.list(containerBill, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:containerBill:list')")
    @ApiOperation("根据id查询拖车运单")
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id") String id) {
        return containerBillService.getById(id);
    }


    @PreAuthorize("hasAuthority('order:containerBill:save')")
    @OperationLog
    @ApiOperation(value ="添加拖车运单")
    @PostMapping()
    public JsonResult save(@RequestBody ContainerBill containerBill) {
        return containerBillService.order(containerBill, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:containerBill:update')")
    @OperationLog
    @ApiOperation(value ="修改拖车运单")
    @PutMapping()
    public JsonResult update(@RequestBody ContainerBill containerBill) {
        return containerBillService.update(containerBill, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:containerBill:remove')")
    @OperationLog
    @ApiOperation(value ="删除拖车运单")
    @DeleteMapping("/{id}")
    public JsonResult remove(@PathVariable("id") String id) {
        return containerBillService.remove(id, getLoginUser());
    }


    @PreAuthorize("hasAuthority('order:containerBill:order')")
    @OperationLog
    @ApiOperation(value ="下单")
    @PostMapping("order")
    public JsonResult order(@RequestBody ContainerBill containerBill) {
        return containerBillService.order(containerBill, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:containerBill:platReceive')")
    @OperationLog
    @ApiOperation(value ="接单",notes = "" +
            "id \n" +
            "state \n" +
            "")
    @PutMapping("platReceive")
    public JsonResult platReceive(@RequestBody ContainerBill containerBill) {
        return containerBillService.updateState(containerBill, getLoginUser());
    }

//    @PreAuthorize("hasAuthority('order:containerBill:platReceive')")
    @OperationLog
    @ApiOperation(value ="现金单",notes = "" +
            "id \n" +
            "cash \n" +
            "")
    @PutMapping("cash")
    public JsonResult cash(@RequestBody ContainerBill containerBill) {
        return containerBillService.cash(containerBill, getLoginUser());
    }

//    @PreAuthorize("hasAuthority('order:containerBill:platReceive')")
    @OperationLog
    @ApiOperation(value ="落箱",notes = "" +
            "id \n" +
            "dropBox \n" +
            "")
    @PutMapping("dropBox")
    public JsonResult dropBox(@RequestBody ContainerBill containerBill) {
        return containerBillService.dropBox(containerBill, getLoginUser());
    }

//    @PreAuthorize("hasAuthority('order:containerBill:platReceive')")
    @OperationLog
    @ApiOperation(value ="预提",notes = "" +
            "id \n" +
            "withholding \n" +
            "")
    @PutMapping("withholding")
    public JsonResult withholding(@RequestBody ContainerBill containerBill) {
        return containerBillService.withholding(containerBill, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:containerBill:carTeam')")
    @OperationLog
    @ApiOperation(value ="指派车队",notes = "" +
            "id \n" +
            "carTeamId 车队id \n" +
            "carTeam varchar 车队\n" +
            "carNo varchar 车牌\n" +
            "driverId 司机id 必填\n" +
            "driver varchar 司机 必填\n" +
            "driverPhone varchar 司机电话\n" +
            "whiteCard varchar 白卡号 \n" +
            "")
    @PutMapping("carTeam")
    public JsonResult carTeam(@RequestBody ContainerBill containerBill) {
        return containerBillService.carTeam(containerBill, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:containerBill:substitutePhoto')")
    @OperationLog
    @ApiOperation(value = "上传箱号封号预录入",notes = "" +
            "id \n" +
            "boxNum varchar 箱号 必填\n" +
            "boxNumPic varchar 箱号图片 必填\n" +
            "sealno varchar 封号 必填\n" +
            "sealnoPic varchar 封号图片 必填\n" +
            "preEntryPic varchar 预录入照片 必填\n" +
            "")
    @PutMapping("substitutePhoto")
    public JsonResult substitutePhoto(@RequestBody ContainerBill containerBill) {
        return containerBillService.update(containerBill, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:containerBill:upBox')")
    @OperationLog
    @ApiOperation(value = "提箱",notes = "" +
        "id \n" +
        "boxNum varchar 箱号 必填\n" +
        "boxNumPic varchar 箱号图片 必填\n" +
        "sealno varchar 封号 必填\n" +
        "sealnoPic varchar 封号图片 必填\n" +
        "arrivalPort:进港码头\n" +
//            "remark:备注\n" +
        "")
    @PutMapping("upBox")
    public JsonResult upBox(@RequestBody ContainerBill containerBill) {
        return containerBillService.update(containerBill, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:containerBill:arrivedFactory')")
    @OperationLog
    @ApiOperation(value ="到厂",notes = "" +
            "id \n" +
            "arrivedFactoryLatitude varchar 到厂纬度 必填\n" +
            "arrivedFactoryLongitude varchar 到厂经度 必填\n" +
            "arrivedFactoryDate varchar 到厂时间 必填\n" +
//            "remark:备注\n" +
            "")
    @PutMapping("arrivedFactory")
    public JsonResult arrivedFactory(@RequestBody ContainerBill containerBill) {
        return containerBillService.update(containerBill, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:containerBill:leaveFactory')")
    @OperationLog
    @ApiOperation(value ="离厂",notes = "" +
            "id \n" +
            "leavedFactoryDate varchar 离厂时间 必填\n" +
            "leavedFactoryLongitude varchar 离厂经度 必填\n" +
            "leavedFactoryLatitude varchar 离厂纬度 必填\n" +
//            "remark:备注\n" +
            "")
    @PutMapping("leaveFactory")
    public JsonResult leaveFactory(@RequestBody ContainerBill containerBill) {
        return containerBillService.update(containerBill, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:containerBill:inPort')")
    @OperationLog
    @ApiOperation(value ="进港",notes = "" +
            "id \n" +
            "arrivedHarbourDate varchar 到港时间 必填\n" +
            "arrivedHarbourLatitude varchar 到港纬度 必填\n" +
            "arrivedHarbourLongitude varchar 到港经度 必填\n" +
            "preEntryPic varchar 预录入照片 必填\n" +
//            "remark:备注\n" +
            "")
    @PutMapping("inPort")
    public JsonResult inPort(@RequestBody ContainerBill containerBill) {
        return containerBillService.update(containerBill, getLoginUser());
    }

    //下单,接单,指派车队,提箱,到厂,离厂,进港,发起费用清单,应付费用审核,应收费用审核,结单
    @PreAuthorize("hasAuthority('order:containerBill:costBill')")
    @OperationLog
    @ApiOperation(value ="发起费用清单",notes = "" +
            "id \n" +
            "state 发起费用清单\n" +
            "")
    @PutMapping("costBill")
    public JsonResult costBill(@RequestBody ContainerBill containerBill) {
        return containerBillService.updateState(containerBill, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:containerBill:payableVerify')")
    @OperationLog
    @ApiOperation(value ="应付费用审核",notes = "" +
            "id \n" +
            "state 应付费用审核\n" +
            "")
    @PutMapping("payableVerify")
    public JsonResult payableVerify(@RequestBody ContainerBill containerBill) {
        return containerBillService.payReceivableVerify(containerBill, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:containerBill:receivableVerify')")
    @OperationLog
    @ApiOperation(value ="应收费用审核",notes = "" +
            "id \n" +
            "state 应收费用审核\n" +
            "")
    @PutMapping("receivableVerify")
    public JsonResult receivableVerify(@RequestBody ContainerBill containerBill) {
        return containerBillService.payReceivableVerify(containerBill, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:containerBill:update')")
    @OperationLog
    @ApiOperation(value ="运单状态修改",notes = "" +
            "id: bill id\n" +
            "state: 状态\n" +
            "")
    @PutMapping("updateState")
    public JsonResult updateState(@RequestBody ContainerBill containerBill) {
        return containerBillService.updateState(containerBill, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:containerBill:update')")
    @OperationLog
    @ApiOperation(value ="结单修改",notes = "" +
            "")
    @PutMapping("finishState")
    public JsonResult finishState(@RequestBody ContainerBill containerBill) {
        return containerBillService.finishState(containerBill, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:containerBill:list')")
    @OperationLog
    @ApiOperation(value ="根据id查询拖车海运报关关联表 ",notes = "" +
            "id:  id\n" +
            "")
    @GetMapping("/getContainerOceanApply")
    public JsonResult getContainerOceanApply(ContainOceanApply containOceanApply) {
        try {
            return containerBillService.getContainerOceanApplyBy(containOceanApply);
        } catch (EmptyResultDataAccessException e) {
            return JsonResult.ok("没有数据");
        }
    }

    @PreAuthorize("hasAuthority('order:containerBill:list')")
    @ApiOperation("保存拖车海运报关关联表")
    @PostMapping("/saveContainerOceanApply")
    public JsonResult saveContainerOceanApply(@RequestBody ContainOceanApply containOceanApply) {
        return containerBillService.saveContainerOceanApply(containOceanApply);
    }

    @PreAuthorize("hasAuthority('order:containerBill:list')")
    @ApiOperation("删除拖车海运报关关联表")
    @PostMapping("/removeContainerOceanApply")
    public JsonResult removeContainerOceanApply(@RequestBody ContainOceanApply containOceanApply) {
        return containerBillService.removeContainerOceanApply(containOceanApply);
    }

    @PreAuthorize("hasAuthority('order:customer:exportsFinance')")
    @ApiOperation("应收应付分类导出财务报表")
    @GetMapping("/exportsFinance")
    public void exportsFinance(ContainerBill containerBill, HttpServletResponse response, HttpServletRequest request) throws Exception{
        containerBillService.exportsFinance(containerBill, getLoginUser(),response, request);
    }

}
